package com.sijia.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sijia.reggie.comment.CustomerException;
import com.sijia.reggie.comment.R;
import com.sijia.reggie.entity.Category;
import com.sijia.reggie.entity.Dish;
import com.sijia.reggie.entity.Setmeal;
import com.sijia.reggie.mapper.CategoryMapper;
import com.sijia.reggie.service.CategoryService;
import com.sijia.reggie.service.DishService;
import com.sijia.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 杨思佳
 * @version 1.0
 */

@Service
public class CategoryServiceImpl
        extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper cm;

    @Autowired
    private DishService ds;

    @Autowired
    private SetmealService ss;

    @Override
    public void deleteCategory(Long id) {
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId, id);
        int count01 = ds.count(lambdaQueryWrapper);
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        int count02 = ss.count(setmealLambdaQueryWrapper);
        if (count01 > 0 || count02 > 0){
            throw new CustomerException("该分类有关联菜品或套餐, 不能删除");
        }
        super.removeById(id);
    }

    @Override
    public List<Category> findByType(Integer type) {
        return cm.findByType(type);
    }
}
